package com.ontheroad.mysql.choice.service;

import cn.modoumama.page.PageObject;
import com.ontheroad.api.request.BannerRequest;
import com.ontheroad.api.request.Request;
import com.ontheroad.api.response.Response;
import com.ontheroad.banner.model.BannerModel;
import com.ontheroad.banner.service.BannerService;
import com.ontheroad.choice.service.AppChoiceService;
import com.ontheroad.circle.model.CircleModel;
import com.ontheroad.circle.service.CircleService;
import com.ontheroad.mysql.user.mapper.UserMapper;
import com.ontheroad.topic.model.TopicModel;
import com.ontheroad.topic.service.TopicService;
import com.ontheroad.user.model.UserModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 精选模块服务
 * Created by kedong on 2017/10/12 0012.
 */
@Service
@Transactional
public class AppChoiceServiceImpl implements AppChoiceService {
    private Logger log = LoggerFactory.getLogger(getClass());

    @Autowired
    private BannerService bannerService;

    @Autowired
    private TopicService topicService;

    @Autowired
    private CircleService circleService;

    @Autowired
    private UserMapper userMapper;

    /**
     * 轮播图列表
     *
     * @return
     */
    @Override
    public Response getBannerList(BannerRequest request) {
        Response response = new Response();
        Map<String, Object> map = new HashMap<>();
        map.put("isPublish", 1);
        map.put("type", request.getType());
        List<BannerModel> list = bannerService.findModelsByCondition(map);
        for (BannerModel bannerModel : list) {
            bannerModel.setContent(null);
        }
        response.addResultData("list", list);
        return response;
    }

    /**
     * 获取热门话题
     *
     * @return
     */
    @Override
    public Response getHotTopic(Request request) {
        Response response = new Response();
        Map<String, Object> query = new HashMap<>();
        query.put("isHot", true);
        List<TopicModel> list = topicService.findModelsByCondition(query);
        response.addResultData("list", list);
        return response;
    }

    /**
     * 精选动态
     *
     * @return
     */
    @Override
    public Response getChoiceCircle(Request request) {
        Response response = new Response();
        PageObject po = new PageObject();
        po.setCurrPage(request.getPageNum());
        po.setPageSize(request.getPageSize());
        po.addCondition("careChosen", 1);
        List<CircleModel> list = circleService.findModelsByCondition(po);
        Integer circleUserId;
        UserModel user;
        for (CircleModel circleModel : list) {
            circleUserId = circleModel.getUserid();
            user = userMapper.getObjectById(circleUserId);
            if (user != null) {
                circleModel.setNick(user.getNick());
            }
        }
        response.addResultData("list", list);
        return response;
    }
}
